import token from '@/utils/token'
import config from '@/config'
import * as userApi from '@/api/sys/user'

import session from '@/utils/session'

const ACCOUNT_KEY = 'accountId'
const MENU_KEY = 'menuList'
const NAME_KEY = 'userTrueName'
const ACCOUNT_TYPE = 'accountType'
const USER_TYPE_KEY = 'userType'
const USERID_KEY = 'userId'

const user = {
    namespaced: true,
    state: {
        accountId: session.get(ACCOUNT_KEY) || '',
        userId: session.get(USERID_KEY) || '',
        accountType: session.get(ACCOUNT_TYPE) || '',
        userType: session.get(USER_TYPE_KEY) || '',
        trueName: session.get(NAME_KEY) || '',
        avatar: 'https:// i.loli.net/2018/08/18/5b7819891bab1.jpg',
        token: token.get(),
        macAddr: session.get(config.macKey) || '',
        menus: session.get(MENU_KEY) || []
    },
    mutations: {
        setAccountId: (state, accountId) => {
            state.accountId = accountId
            session.set(ACCOUNT_KEY, accountId)
        },
        setUserId: (state, userId) => {
            state.userId = userId
            session.set(USERID_KEY, userId)
        },
        setAccountType: (state, accountType) => {
            state.accountType = accountType
            session.set(ACCOUNT_TYPE, accountType)
        },
        setUserType: (state, userType) => {
            state.userType = userType
            session.set(USER_TYPE_KEY, userType)
        },
        setTrueName: (state, trueName) => {
            state.trueName = trueName
            session.set(NAME_KEY, trueName)
        },
        setStoreId: (state, storeId) => {
            state.storeId = storeId
            session.set(STOREID, storeId)
        },
        setIsShop: (state, isShop) => {
            state.isShop = isShop
            session.set(ISSHOP, isShop)
        },
        setIsMainShop: (state, isMainShop) => {
            state.isMainShop = isMainShop
            session.set(ISMAINSHOP, isMainShop)
        },
        setAvatar: (state, avatar) => {
            state.avatar = avatar
        },

        setToken: (state, data) => {
            if (data) {
                token.set(data)
            } else {
                console.log("到这里删除TOKEN")
                token.remove()
            }
            state.token = data
        },
        setMacAddr: (state, macAddr) => {
            state.macAddr = macAddr
            session.set(config.macKey, macAddr)
        },
        setMenus: (state, menus) => {
            state.menus = menus
            session.set(MENU_KEY, menus)
        }
    },
    actions: {
        loginHandler({commit}, {username, password}) {
            return new Promise((resolve, reject) => {
                userApi.login({username, password})
                    .then(resp => {
                        if (resp.code === 200) {
                            commit('setToken', resp.data.jwtToken)
                            commit('setTrueName', resp.data.name)
                            commit('setAccountId', resp.data.accountId)
                            commit('setUserId', resp.data.userId)
                            resolve(resp)
                        } else {
                            reject(resp)
                        }
                    })
                    .catch(err => {
                        reject(err)
                    })
            })
        },
        logoutHandler({commit}) {
            return new Promise((resolve, reject) => {
                userApi
                    .logout()
                    .then(() => {
                        commit('setToken', '')
                        resolve()
                    })
                    .catch(err => {
                        reject(err)
                    })
            })
        }
    }

}


export default user
